/*
 * ClientManager.java
 *
 * $Id: ClientManager.java,v 1.8 2009-09-17 12:08:20 mario Exp $
 */
package org.ceteca.explica.client;


import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.client.gui.ExplicaInitView;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.javaglade.Gui;
import org.ceteca.javaglade.toolkit.swt.StatusBar;
import org.ceteca.javaglade.toolkit.swt.SwtToolkit;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

/**
* Main client class that starts the client side of the application.
* <br/>
* Responsibilities:
* <ul>
*  <li>Configure the client context of the application.</li>
*  <li>Init the resources installation process.</li>
*  <li>Init the main screen building process.</li>
* </ul>
* <br/>
* @author Mario García García <mario@imagos.es>
 * Copyright (c) 2008 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ClientManager {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Init the client context with the generic objects that will be used by the
		// client instances.
		ClientContext.getInstance().initClientContext();

		// Build the initializing progress window
		ExplicaInitView.getInstance().buildInitView();
		Gui initGui = ExplicaGUIBuilder.getInstance().getGui(InterfaceExplica.EXPLICA_INIT_GLADE_NAME);
		Shell initWindow = initGui.getWindow(InterfaceExplica.EXPLICA_INIT_WINDOW_NAME).getShell();
		ProgressBar progressBar = (ProgressBar)initGui.getWidget(InterfaceExplica.EXPLICA_INIT_PROGRESSBAR_NAME);
		StatusBar statusBar = (StatusBar)initGui.getWidget(InterfaceExplica.EXPLICA_INIT_STATUSBAR_NAME);
		
		// Start the configuration of the client and build the GUI
	    new InitClientOperation(SwtToolkit.getDisplay(), progressBar, initWindow, statusBar).start();
	    
	    initWindow.pack();
		initWindow.open();
		while (!initWindow.isDisposed()) {
			if (!SwtToolkit.getDisplay().readAndDispatch()) {
				SwtToolkit.getDisplay().sleep();
			}
		}

		ClientContext.getInstance().getLogger().info("Client initialization completed!");
		ExplicaGUIBuilder.getInstance().printGUIs(ClientContext.getInstance().getLogger());
		ClientContext.getInstance().printEntityLaunchers();
		
		// Show the Explica core GUI to the user
		Gui coreGui = ExplicaGUIBuilder.getInstance().getGui(InterfaceExplica.EXPLICA_CORE_GLADE_NAME);
		Shell mainWindow = coreGui.getWindow(InterfaceExplica.EXPLICA_MAIN_WINDOW_NAME).getShell();
		ClientContext.getInstance().getLogger().debug("Mostrando arbol de widgets &&&");
		coreGui.printWidgets(ClientContext.getInstance().getLogger());
		ClientContext.getInstance().getLogger().debug("Fin arbol de widgets &&&");
		mainWindow.open();
		while (!mainWindow.isDisposed()) {
			if (!SwtToolkit.getDisplay().readAndDispatch()) {
				SwtToolkit.getDisplay().sleep();
			}
		}
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ClientManager.java,v 1.8 2009-09-17 12:08:20 mario Exp $";
	}
}